package 死锁;

public class _01_死锁 {

    public static void main(String[] args) {
        Object o1 = new Object();
        Object o2 = new Object();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o1){
                    try {
                        System.out.println("线程1 拿到锁1了");
                        Thread.sleep(1000);
                        synchronized (o2){
                            System.out.println("线程2 拿到锁2了");
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (o2){
                    try {
                        System.out.println("线程2 拿到锁2了");
                        Thread.sleep(1000);
                        synchronized (o1){
                            System.out.println("线程2 拿到锁1了");
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        });

        t1.start();
        t2.start();
    }
}
